cmake_minimum_required(VERSION 2.6)

project(mns_sdk C CXX)

IF (ENABLE_COVERAGE)
    SET(CMAKE_CXX_FLAGS "-g -Wall -O0 -fPIC -DMUDUO_STD_STRING -DHAVE_NETINET_IN_H")
ELSE ()
    SET(CMAKE_CXX_FLAGS "-g -Wall -O2 -fPIC -DMUDUO_STD_STRING -DHAVE_NETINET_IN_H")
ENDIF ()

MESSAGE(STATUS "CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}")

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
MESSAGE(STATUS "CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}")

if (CMAKE_BUILD_INIT)
    FIND_PACKAGE(Muduo REQUIRED)
else()
    find_package(Boost REQUIRED)
    find_package(ZLIB REQUIRED)
    find_path(LOG4CPLUS_INCLUDE_DIR log4cplus)
    find_library(LOG4CPLUS_LIBRARY NAMES log4cplus)
    find_program(THRIFT_COMPILER thrift)
    find_path(THRIFT_INCLUDE_DIR thrift/Thrift.h)
    find_library(THRIFT_LIBRARY NAMES thrift)
    find_library(MUDUO_BASE_LIBRARY libmuduo_base.a)
    find_library(MUDUO_NET_LIBRARY libmuduo_net.a)
    find_library(MUDUO_HTTP_LIBRARY libmuduo_http.a)
    find_path(MUDUO_INCLUDE_DIR muduo)

    if(ZLIB_FOUND)
        MESSAGE(STATUS "found zlib  ${ZLIB_FOUND}")
    else()
        MESSAGE(STATUS "no found zlib")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(THRIFT_COMPILER AND THRIFT_INCLUDE_DIR AND THRIFT_LIBRARY)
        MESSAGE(STATUS "found thrift")
    else()
        MESSAGE(STATUS "no found thrift")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(MUDUO_BASE_LIBRARY AND MUDUO_HTTP_LIBRARY AND MUDUO_NET_LIBRARY AND MUDUO_INCLUDE_DIR)
        MESSAGE(STATUS "found muduo")
    else()
        MESSAGE(STATUS "no found muduo")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(LOG4CPLUS_INCLUDE_DIR AND LOG4CPLUS_LIBRARY)
        MESSAGE(STATUS "found log4cplus")
    else()
        MESSAGE(STATUS "no found log4cplus")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(Boost_INCLUDE_DIRS)
        MESSAGE(STATUS "found boost")
    else()
        MESSAGE(STATUS "no found boost")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(SOME_NO_FOUND)
        MESSAGE(FATAL_ERROR "Dependency not found please confirm")
    endif()

    MESSAGE(STATUS "THRIFT_COMPILER         ${THRIFT_COMPILER}")
    MESSAGE(STATUS "THRIFT_INCLUDE_DIR      ${THRIFT_INCLUDE_DIR}")
    MESSAGE(STATUS "LOG4CPLUS_INCLUDE_DIR   ${LOG4CPLUS_INCLUDE_DIR}")
    MESSAGE(STATUS "ZLIB_INCLUDE_DIR        ${ZLIB_INCLUDE_DIR}")
    MESSAGE(STATUS "Boost_INCLUDE_DIRS      ${Boost_INCLUDE_DIRS}")
    MESSAGE(STATUS "ZLIB_LIBRARY            ${ZLIB_LIBRARY}")
    MESSAGE(STATUS "THRIFT_LIBRARY          ${THRIFT_LIBRARY}")
    MESSAGE(STATUS "LOG4CPLUS_LIBRARY       ${LOG4CPLUS_LIBRARY}")
    MESSAGE(STATUS "Boost_INCLUDE_DIRS      ${Boost_INCLUDE_DIRS}")
    MESSAGE(STATUS "MUDUO_BASE_LIBRARY      ${MUDUO_BASE_LIBRARY}")
    MESSAGE(STATUS "MUDUO_NET_LIBRARY       ${MUDUO_NET_LIBRARY}")
    MESSAGE(STATUS "MUDUO_HTTP_LIBRARY      ${MUDUO_HTTP_LIBRARY}")

    INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
    INCLUDE_DIRECTORIES(${THRIFT_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${THRIFT_INCLUDE_DIR}/thrift)
    INCLUDE_DIRECTORIES(${LOG4CPLUS_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})
    INCLUDE_DIRECTORIES(${MUDUO_INCLUDE_DIR})

    SET(MNS_SRC_PATH ${CMAKE_SOURCE_DIR})
    MESSAGE(STATUS "MNS_SRC_PATH ${MNS_SRC_PATH}")

    #for output
    EXECUTE_PROCESS(COMMAND mkdir ${CMAKE_SOURCE_DIR}/build)
    SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/build)
    MESSAGE(STATUS "CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}")

    EXECUTE_PROCESS(COMMAND mkdir ${CMAKE_INSTALL_PREFIX}/include)
    SET(INCLUDE_OUTPUT_PATH ${CMAKE_INSTALL_PREFIX}/include)
    MESSAGE(STATUS "INCLUDE_OUTPUT_PATH ${INCLUDE_OUTPUT_PATH}")

    EXECUTE_PROCESS(COMMAND mkdir ${CMAKE_INSTALL_PREFIX}/lib)
    SET(LIBRARY_OUTPUT_PATH ${CMAKE_INSTALL_PREFIX}/lib)
    MESSAGE(STATUS "LIBRARY_OUTPUT_PATH ${LIBRARY_OUTPUT_PATH}")

    EXECUTE_PROCESS(COMMAND mkdir ${CMAKE_INSTALL_PREFIX}/bin)
    SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_INSTALL_PREFIX}/bin)
    MESSAGE(STATUS "EXECUTABLE_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH}")

    EXECUTE_PROCESS(COMMAND mkdir ${INCLUDE_OUTPUT_PATH}/mns_sdk)
    SET(INCLUDE_MNS_OUTPUT_PATH ${INCLUDE_OUTPUT_PATH}/mns_sdk)
    MESSAGE(STATUS "INCLUDE_MNS_OUTPUT_PATH ${INCLUDE_MNS_OUTPUT_PATH}")

    EXECUTE_PROCESS(COMMAND mkdir ${INCLUDE_OUTPUT_PATH}/mns_sdk/util)
    SET(INCLUDE_MNS_UTIL_OUTPUT_PATH ${INCLUDE_OUTPUT_PATH}/mns_sdk/util)
    MESSAGE(STATUS "INCLUDE_MNS_UTIL_OUTPUT_PATH ${INCLUDE_MNS_UTIL_OUTPUT_PATH}")


    SET(OCTO_IDL_PATH ${CMAKE_SOURCE_DIR}/../../common/idl-mns)
    MESSAGE(STATUS "OCTO_IDL_PATH ${OCTO_IDL_PATH}")

    SET(INCLUDE_OCTOIDL_OUTPUT_PATH ${INCLUDE_OUTPUT_PATH}/octoidl)
    MESSAGE(STATUS "INCLUDE_OCTOIDL_OUTPUT_PATH ${INCLUDE_OCTOIDL_OUTPUT_PATH}")
    EXECUTE_PROCESS(COMMAND mkdir ${INCLUDE_OCTOIDL_OUTPUT_PATH})

    INCLUDE_DIRECTORIES(SYSTEM /usr/include/)
    INCLUDE_DIRECTORIES(SYSTEM ${INCLUDE_OUTPUT_PATH})

    ADD_SUBDIRECTORY(thrid/octoidl)
    ADD_SUBDIRECTORY(src)

    if (NOT CMAKE_BUILD_NO_EXAMPLES)
        ADD_SUBDIRECTORY(example)
    endif ()
endif()



